<?php

	//header('Content-type: application/json');

try
{

	$con = mysql_connect("localhost","root","");
	mysql_select_db("studyking", $con);
	//include('admindbconnect.class.php'); 
	// a new database class
	//$mysqli = new admindatabase();
	mysql_query("SET NAMES 'uft8'");
	mysql_query("set character set 'utf8'");
	if($_GET["action"] == "list")
	{
        //Get record count
        $result = mysql_query("SELECT COUNT(*) AS RecordCount FROM details;");
        $row = mysql_fetch_array($result);
        $recordCount = $row['RecordCount'];

        //Get records from database
        $result = mysql_query("SELECT d.id,c.name as category,d.name,d.description,d.link,d.photo,d.rate FROM category_details as cd, details as d,category as c where cd.id_details=d.id and cd.id_category=c.id ORDER BY " .
            $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] . ";");

		//Add all records to an array
		$rows = array();
		while($row = mysql_fetch_array($result))
		{
		    $rows[] = $row;
		}

		//Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['TotalRecordCount'] = $recordCount;
        $jTableResult['Records'] = $rows;
		print json_encode($jTableResult);
	}
	//Creating a new record (createAction)
	
	else if($_GET["action"] == "create")
	{
		//Insert record into database
		$result = mysql_query("INSERT INTO details(name, description, link, photo, rate) VALUES('" . $_POST["name"] . "', '" . $_POST["description"] . "','".$_POST["link"]."','" . $_POST["photo"]. "',0);");
		
		//Get last inserted record (to return to jTable)
		$result = mysql_query("SELECT * FROM details where id=LAST_INSERT_ID();");
		$row = mysql_fetch_array($result);


		//Return result to jTable
		$jTableResult = array();
		$jTableResult['Result'] = "OK";
		$jTableResult['Record'] = $row;
		print json_encode($jTableResult);
	}
	//Updating a record (updateAction)

	else if($_GET["action"] == "update")
	{
		//Update record in database
		$result = mysql_query("UPDATE people SET name = '" . $_POST["name"] . "', link = " . $_POST["link"] .
            "', description = " . $_POST["description"] .  "', photo = " . $_POST["photo"] .
            " WHERE id = " . $_POST["id"] . ";");

		//Return result to jTable
		$jTableResult = array();
		$jTableResult['Result'] = "OK";
		print json_encode($jTableResult);
	}

	//Deleting a record (deleteAction)
	else if($_GET["action"] == "delete")
	{
		//Delete from database
		$result = mysql_query("DELETE FROM details WHERE id = " . $_POST["id"] . ";");

		//Return result to jTable
		$jTableResult = array();
		$jTableResult['Result'] = "OK";
		print json_encode($jTableResult);
	}
    else if($_GET["action"] == "amount")
    {
        //Get record count
        $result = mysql_query("SELECT COUNT(*) AS RecordCount FROM details;");
        $row = mysql_fetch_array($result);
        $recordCount = $row['RecordCount'];
        print json_encode($recordCount);
    }
	//Close database connection
	mysql_close($con);

}
catch(Exception $ex)
{
	    //Return error message
	$jTableResult = array();
	$jTableResult['Result'] = "ERROR";
	$jTableResult['Message'] = $ex->getMessage();
	print json_encode($jTableResult);
}

?>